#include "minSphere.h"
#include <CGAL/Cartesian_d.h>
#include <CGAL/Random.h>
#include <CGAL/Gmpq.h>
#include <CGAL/Min_sphere_of_spheres_d.h>
#include <vector>

//typedef CGAL::Gmpq                        FT;
typedef double                          FT;
typedef CGAL::Cartesian_d<FT>             K;
typedef CGAL::Min_sphere_of_spheres_d_traits_d<K,FT,3> Traits;
typedef CGAL::Min_sphere_of_spheres_d<Traits> Min_sphere;
typedef K::Point_d                        Point;
typedef Traits::Sphere                    Sphere;


MinSphere MinSphere::computePoints (vector<Point3d>& points)
{
    std::vector<Sphere> S;
    FT coord[3];

    for (unsigned i=0; i<points.size(); ++i)
    {
        for (int j=0; j<3; ++j)
            coord[j] = points[i][j];
        Point p(3,coord,coord+3);
        S.push_back(Sphere(p,0));
    }

    Min_sphere ms(S.begin(),S.end());
    CGAL_assertion(ms.is_valid());

    MinSphere ret;
    ret.radius = ms.radius();
    Traits::Cartesian_const_iterator it = ms.center_cartesian_begin();
    ret.center[0] = *(it++);
    ret.center[1] = *(it++);
    ret.center[2] = *it;
    return ret;
}

MinSphere MinSphere::computeVertices (vector<CVertex*>& vv)
{
    std::vector<Sphere> S;
    FT coord[3];

    for (unsigned i = 0; i < vv.size(); i++)
    {
        for (int j = 0; j < 3; j++)
            coord[j] = (vv[i]->P())[j];

        Point p (3, coord, coord+3);
        S.push_back (Sphere(p,0));
    }

    Min_sphere ms (S.begin(), S.end());
    CGAL_assertion (ms.is_valid());

    MinSphere ret;
    ret.radius = ms.radius();
    Traits::Cartesian_const_iterator it = ms.center_cartesian_begin();
    ret.center[0] = *(it++);
    ret.center[1] = *(it++);
    ret.center[2] = *it;
    return ret;
}
